//
//  ESTDayWeekMonthView.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/9/29.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTDayWeekMonthView.h"

@interface ESTDayWeekMonthView ()
/**
 *  记录点击的第几个
 */
@property(nonatomic, assign) NSInteger integer;
/**
 *  日周年
 */
@property(nonatomic, strong) NSArray<NSString *> *dateText;
@end

@implementation ESTDayWeekMonthView

- (instancetype)initWithDateType:(NSInteger)dateType {
    if (self = [super init]) {
        self.integer = dateType;
    }
    return self;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    [self setUI];
}
- (void)setUI {
    CGFloat width = DateView_ChildViewWidth;
    for (NSInteger i = 0; i < 3; i++) {
        TYZView *view = [[TYZView alloc] init];
        [self addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset((width + DateView_Padding) * i);
            make.top.bottom.equalTo(self);
            make.width.mas_equalTo(width);
        }];
        view.backgroundColor = [UIColor clearColor];
        
        TYZView *spot = [[TYZView alloc] init];
        [view addSubview:spot];
        NSInteger number = i == self.integer ? DateView_SpotHieght * 2 : DateView_SpotHieght;
        [spot mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(number);
            if (i == self.integer) {
                make.left.right.equalTo(view);
                make.bottom.equalTo(view);
            } else {
                make.width.mas_equalTo(number);
                make.centerX.equalTo(view);
                make.bottom.equalTo(view).offset(number * -0.5);
            }
        }];
        spot.layer.cornerRadius = number * 0.5;
        
        TYZLabel *label = [TYZLabel labelWithText:self.dateText[i]];
        [view addSubview:label];
        label.textAlignment = NSTextAlignmentCenter;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(view);
            make.bottom.equalTo(spot.mas_top).offset(-10);
        }];
        if (i == self.integer) {
            spot.backgroundColor = TYZWhiteColor;
            label.textColor = TYZWhiteColor;
            label.font = [UIFont systemFontOfSize:CoreInfo.fontSize * 1.5 weight:0.2];
        } else {
            spot.backgroundColor = Activity_Home_Text_Color;
            label.textColor = Activity_Home_Text_Color;
            label.font = [UIFont systemFontOfSize:CoreInfo.fontSize * 1.2];
        }
        
        TYZButton *button = [[TYZButton alloc] init];
        [view addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        button.tag = TAG_SIXTH_ACTIVITY_DayWeekMonthTags + i;
        [button addTarget:self action:@selector(didClickCoverButton:) forControlEvents:UIControlEventTouchUpInside];
    }
}
- (void)didClickCoverButton:(UIButton *)button {
    self.integer = button.tag - TAG_SIXTH_ACTIVITY_DayWeekMonthTags;
}
- (void)setInteger:(NSInteger)integer {
    _integer = integer;
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[TYZView class]] || [obj isKindOfClass:[TYZButton class]]) {
            [obj removeFromSuperview];
        }
    }];
    [self setUI];
    if (self.didClickDayWeekMonthBlock) {
        self.didClickDayWeekMonthBlock(self.integer);
    }
}
- (NSArray<NSString *> *)dateText {
    if (_dateText == nil) {
        _dateText = @[@"日", @"周", @"月"];
    }
    return _dateText;
}
@end
